package com.wgz.mp.generator.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.wgz.mp.generator.handler.MyMetaObjectHandler;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;

/**
 * @author wgz
 * @title
 * @Package PACKAGE_NAME
 * @data 2021/4/1 20:20
 */
@Configuration
@ComponentScan(value = "com.wgz.mp.generator",
        excludeFilters = { @ComponentScan.Filter(type= FilterType.ANNOTATION,classes={Controller.class}),
        })
@PropertySource("classpath:jdbc.properties")
@MapperScan("com.wgz.mp.generator.mapper")
public class SpringConfig {

    @Value("${jdbc.url}")
    public String url;
    @Value("${jdbc.username}")
    public String username;
    @Value("${jdbc.password}")
    public String password;
    @Value("${jdbc.driver}")
    public String driverClassName;
    // xml

    @Bean("dataSource")
    public DruidDataSource druidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setDriverClassName(driverClassName);
        return druidDataSource;
    }

    @Bean("sqlSessionFactory")
    public MybatisSqlSessionFactoryBean sqlSessionFactory(){
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        // OptimisticLockerInterceptor @Version  分页PaginationInterceptor
        mybatisSqlSessionFactoryBean.setPlugins(new OptimisticLockerInterceptor(),new PaginationInterceptor());
        //扫描枚举类
        mybatisSqlSessionFactoryBean.setTypeEnumsPackage("com.wgz.mp.generator.mapper");
        //注入数据源
        mybatisSqlSessionFactoryBean.setDataSource(druidDataSource());
        //配置configuration
        mybatisSqlSessionFactoryBean.setConfiguration(configLocation());

        return mybatisSqlSessionFactoryBean;
    }

    @Bean("configLoation")
    public MybatisConfiguration configLocation(){
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        // 日志
        mybatisConfiguration.setLogImpl(StdOutImpl.class);
        //  全局 配置
        mybatisConfiguration.setGlobalConfig(globalConfig());
        return mybatisConfiguration;

    }

    @Bean("globalConfig")
    public GlobalConfig globalConfig(){
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setDbConfig(dbConfig());
        globalConfig.setMetaObjectHandler(new MyMetaObjectHandler());
        return globalConfig;
    }
    @Bean("dbCofig")
    public GlobalConfig.DbConfig dbConfig() {
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setLogicDeleteValue("1");
        dbConfig.setLogicNotDeleteValue("0");
        return dbConfig;
    }



    @Bean("transactionManager")
    public DataSourceTransactionManager dataSourceTransactionManager(){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(druidDataSource());
        return dataSourceTransactionManager;
    }

}
